<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="keyword" placeholder="操作的类名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="warning" v-on:click="keywordQuery">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表v-loading="listLoading"
    :data="tableDatas" -表格数据
    v-loading="listLoading" -> 加载圈
    @selection-change="handleSelectionChange" -> 批量删除
    highlight-current-row  -> 选择一行数据，变亮
    -->

    <el-table :data="tableDatas" v-loading="listLoading"
              @selection-change="handleSelectionChange" highlight-current-row
              style="width: 100%;" :row-key="SpreadPages">
      <!--多选框-->
      <el-table-column type="selection" width="55" :reserve-selection="true">
      </el-table-column>
      <el-table-column type="index" width="70" label="序号">
      </el-table-column>

      <el-table-column prop="operId" label="操作人id" width="50">
      </el-table-column>


      <el-table-column prop="operTime" label="操作时间" width="200">
      </el-table-column>

      <el-table-column prop="className" label="操作的类名" width="300">
      </el-table-column>

      <el-table-column prop="methodName" label="操作的方法名" width="200">
      </el-table-column>

      <el-table-column prop="ip" label="本机地址" width="200">
      </el-table-column>


      <el-table-column label="操作">
        <template scope="scope">
          <el-button type="danger" size="small" @click="handleDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" :disabled="this.sels.length === 0" @click="handlePatchDel">批量删除</el-button>
      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange" :current-page="currentPage"
                     :page-sizes="[5, 10, 15]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
                     background
                     :total="totals" style="float:right;">
      </el-pagination>
    </el-col>

  </section>
</template>

<script>

import th from "element-ui/src/locale/lang/th";
import {LinearGradient} from "echarts/lib/util/graphic";

export default {
  data() {
    return {
      //1.分页查询相关数据
      totals: 0,//总记录条数
      currentPage: 1,//当前页,要传递到后台的
      pageSize: 5, //每页显示多少条
      tableDatas: [], //当前页数据

      listLoading: false, //加载圈

      title: '',

      //2.高级查询相关数据
      keyword: '',

      //3.删除相关数据

      //4.批量删除相关数据
      sels: [], //存储多选记录
      

      //6.添加和修改校验规则-表单项校验
      saveFormRules: {
        typeName: [
          {required: true, message: '请输入文章类型名称', trigger: 'blur'}
        ],
        parentId: [
          {required: true, message: '请输入上级id', trigger: 'blur'}
        ],
      },

    }
  },
  methods: {
    //跨页操作
    SpreadPages(row){
      return row.id
    },
    //1.分页查询相关方法
    //1.1.获取列表数据
    getTableData() {
      this.listLoading = true //开启加载圈
      var params = {
        "currentPage": this.currentPage, //点击的当前第几页
        "pageSize": this.pageSize,   //当前页需要显示多少条数据
        "keyword": this.keyword    //模糊查询 ->关键字
      }
      this.$http.post("/log", params).then(request => {
        console.log(request)
        //接收参数添加到表格中(tableDatas),request.data ->固定写法
        this.tableDatas = request.data.list
        this.totals = request.data.total
        this.listLoading = false //关闭加载圈
      })
    },
    //1.2.选择第几页时触发
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage //1 -> 2
      this.getTableData()
    },
    //1.3.选择每页显示记录条数时触发
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.getTableData()
    },

    //2.高级查询相关方法
    keywordQuery() {
      this.currentPage = 1; //从第一页开始查询
      this.getTableData();
    },

    //3.删除相关方法
    //row -> 当前行对象 获取一行数据
    handleDel(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.delete("/log/" + row.id,).then(res => {
          if (res.data.success) {
            this.$message.success("操作删除成功")
            this.getTableData()
          } else {
            this.$message.error("服务器异常，删除失败")
          }
        }).catch(res => {
          this.$message.error("网络异常，请查看您的网络")
        })
      }).catch(() => {
        this.$message.error("已取消删除")
      });
    },

    //4.批量删除相关方法
    //4.1.选项改变时触发
    handleSelectionChange(val) {
      this.sels = val
    },

    //4.2.批量删除
    handlePatchDel() {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = this.sels.map(obj => obj.id)//获取批量删除对象的id，组成数组。
        this.$http.patch("/log", ids).then(res => {
          if (res.data.success) {
            this.$message.success("操作批量删除成功")
            this.getTableData()
          } else {
            this.$message.error("服务器异常，批量删除失败")
          }
        }).catch(res => {
          this.$message.error("网络异常，请查看您的网络")
        })
      }).catch(() => {
        this.$message.error("已取消批量删除")
      });
    },
    

  },
  mounted() {
    this.getTableData();
  }
}

</script>

<style scoped></style>
